package com.vr.xuecheng.content.controller;

import cn.hutool.core.bean.BeanUtil;
import com.vr.xuecheng.base.response.Result;
import com.vr.xuecheng.content.model.dto.CoursePublishDTO;
import com.vr.xuecheng.content.model.entity.CoursePublish;
import com.vr.xuecheng.content.model.vo.CoursePreviewVO;
import com.vr.xuecheng.content.service.ICoursePublishService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author hzh
 * @date 2025/03/17
 * 课程发布
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class CoursePublishController {
    private final ICoursePublishService coursePublishService;
    /**
     * @param courseId 课程id
     * @return {@code ModelAndView } 模型和视图
     * 显示course_template视图
     */
    @GetMapping("/coursepreview/{course-id}")
    public ModelAndView getCourseTemplateView(@PathVariable("course-id") Long courseId) {
        ModelAndView modelAndView = new ModelAndView();
        CoursePreviewVO coursePreviewInfo = coursePublishService.getCoursePreviewInfo(courseId);
        modelAndView.addObject("model",coursePreviewInfo);
        modelAndView.setViewName("course_template");
        return modelAndView;
    }

    /**
     * @param courseId 课程id
     * @return {@code Result<String> } 结果
     * 课程发布
     */
    @Operation(summary = "课程发布")
    @PreAuthorize("hasAuthority('xc_teachmanager_course_publish')")
    @PostMapping("/coursepublish/{course-id}")
    public Result<String> coursePublish(@PathVariable("course-id") Long courseId) {
        return Result.toResult(coursePublishService.publish(courseId));
    }
    @Operation(summary = "通过id获取课程发布信息")
    @GetMapping("/coursepublish/{course-id}")
    public CoursePublishDTO getCoursePublishInfoById(@PathVariable("course-id") Long courseId) {
        return BeanUtil.toBean(coursePublishService.lambdaQuery()
                .eq(CoursePublish::getId, courseId)
                        .eq(CoursePublish::getStatus, "203002")
                .one(),
                CoursePublishDTO.class);
    }
    @Operation(summary = "课程下架")
    @GetMapping("/courseoffline/{course-id}")
    public Result<String> courseOffline(@PathVariable("course-id") Long courseId) {
        return Result.toResult(coursePublishService.courseOffline(courseId));
    }

}
